﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

using Padelcenter.Common;
using Padelcenter.DataAccess;

namespace Padelcenter.BusinessLogic
{
    public class GetMoneda : IBusinessLogic
    {
        #region Private Vars

        private DataSet _ResultSet = null;
        private Moneda _Moneda = null;
        private bool _Disposed = false;

        #endregion

        #region Constructor

        public GetMoneda() 
        { 
        }

        ~GetMoneda()
		{
			this.Dispose(false);
        }

        #endregion

        #region Properties

        public Moneda Moneda
        {
            get { return _Moneda; }
            set { _Moneda = value; }
        }

        public DataSet Resultset
        {
            get { return _ResultSet; }
            set { _ResultSet = value; }
        }

        #endregion             

        #region Public Methods

        public void Invoke()
        {
            Moneda_Select oMonedaSelect = null;
            try
            {
                oMonedaSelect = new Moneda_Select();
                oMonedaSelect.Moneda = this.Moneda;
                this.Resultset = oMonedaSelect.Get();
            }
            catch (Exception e)
            {
                // Common Exception
                throw (new Exception("GetMoneda.Invoke: " + e.Message.ToString()));
            }
            finally
            {
                if (oMonedaSelect != null)
                    oMonedaSelect.Dispose();
                oMonedaSelect = null;
            }
        }

        public List<Moneda> MonedaList()
        {
            List<Moneda> listaMoneda = null;
            DataTable dTable = null;

            try
            {
                // Llamamos al metodo para obtener el Select
                this.Invoke();

                listaMoneda = new List<Moneda>();

                dTable = new DataTable();
                dTable = this.Resultset.Tables[0];

                if (dTable.Rows.Count > 0)
                {
                    Moneda oMoneda = null;
                    foreach (DataRow r in dTable.Rows)
                    {
                        oMoneda = new Moneda();
                        oMoneda.IdMoneda = Convert.ToInt32(r["idMoneda"]);
                        oMoneda.Descripcion = r["Descripcion"].ToString();
                        oMoneda.Cotizacion = Convert.ToDouble(r["Cotizacion"]);
                        oMoneda.Simbolo = r["Simbolo"].ToString();
                        oMoneda.Activo = Convert.ToBoolean(r["Activo"]);

                        listaMoneda.Add(oMoneda);
                    }
                }
                return listaMoneda;
            }
            catch (Exception e)
            {
                 // Common Exception
                throw (new Exception("GetMoneda.MonedaList: " + e.Message.ToString()));
            }
            finally
            {
                if (dTable != null)
                    dTable.Dispose();
                dTable = null;

                listaMoneda = null;
            }
        }

        #endregion       

        #region IDisposable Members

        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposable)
        {
            if (!this._Disposed)
            {
                if (disposable)
                {
                    ReleaseManagedResources();
                }
                ReleaseUnmanagedResources();
            }
            _Disposed = true;
        }

        /// Release unmanaged resources
        protected virtual void ReleaseUnmanagedResources()
        {
            if (_ResultSet != null)
                _ResultSet.Dispose();
            _ResultSet = null;


            if (_Moneda != null)
                _Moneda.Dispose();
            _Moneda = null;

        }

        /// Release managed resources		
        protected virtual void ReleaseManagedResources()
        {
        }

        #endregion
    }
}
